/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[SendingHtmlMails.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.mail;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Before;
import org.junit.Test;

import javamail.utils.MailAuthenticator;
import javamail.utils.MailConstants;
import javamail.utils.PasswordUtils;

/**
 * 使用SMTP协议发送Html格式的邮件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class SendingHtmlMails {

	private static Properties properties = null;

	private static String emailSender = "javamail_sns@sina.com";
	private static String emailRecipient = "18505106250@163.com";

	@Before
	public void init() {
		properties = new Properties();
		properties.setProperty("mail.transport.protocol", MailConstants.PROTOCOL_SMTP);
		properties.setProperty("mail.smtp.host", MailConstants.SMTP_HOST_OF_SINA);
		properties.setProperty("mail.smtp.port", MailConstants.SMTP_PORT_OF_SINA);
		properties.setProperty("mail.smtp.auth", String.valueOf(MailConstants.IS_AUTHORIZATION_NEED));
		properties.setProperty("mail.debug", String.valueOf(MailConstants.IS_DEBUG_MOD_ENABLED));
	}

	/**
	 * 发送Html格式的邮件
	 * 
	 * @throws Exception
	 */
	public static void sendHtmlMail() throws Exception {
		// 创建Session实例
		Session session = Session.getInstance(properties,
				new MailAuthenticator(emailSender, PasswordUtils.readPassword(emailSender)));
		// 创建MimeMessage实例
		MimeMessage message = new MimeMessage(session);

		// 封装Message的属性
		message.setSubject("JavaMail - Html文件");
		message.setFrom(new InternetAddress(emailSender));
		message.setSentDate(new Date());
		message.setRecipient(RecipientType.TO, new InternetAddress(emailRecipient));
		message.setContent("<span style='color:red;'>使用JavaMail发送Html邮件</span>", "text/html;charset=UTF-8");

		Transport.send(message);
	}

	/**
	 * 发送Html附带图片的邮件
	 * 
	 * @throws Exception
	 */
	public static void sendHtmlMailWithImage() throws Exception {
		// 创建Session实例
		Session session = Session.getInstance(properties,
				new MailAuthenticator(emailSender, PasswordUtils.readPassword(emailSender)));
		// 创建MimeMessage实例
		MimeMessage message = new MimeMessage(session);

		// 封装Message的属性
		message.setSubject("JavaMail - 带有图片的Html邮件", "UTF-8");
		message.setFrom(new InternetAddress(emailSender));
		message.setSentDate(new Date());
		message.setRecipient(RecipientType.TO, new InternetAddress(emailRecipient));
		// 抄送
		message.setRecipient(RecipientType.CC, new InternetAddress(emailRecipient));
		// 密送[不会在邮件收件人名单中显示出来]
		message.setRecipient(RecipientType.BCC, new InternetAddress(emailRecipient));
		message.setSentDate(new Date());

		// 创建一个related类型的MimeMultipart实例, related类型用于组合文本和图片
		MimeMultipart mailBodyMP = new MimeMultipart("related");
		// 图片部分
		MimeBodyPart imagePart = new MimeBodyPart();
		// 正文部分
		MimeBodyPart htmlPart = new MimeBodyPart();
		mailBodyMP.addBodyPart(imagePart);
		mailBodyMP.addBodyPart(htmlPart);

		// 图片
		DataSource dataSource = new FileDataSource(new File("src/javamail/attachments/1.jpg"));
		DataHandler dataHandler = new DataHandler(dataSource);
		imagePart.setDataHandler(dataHandler);
		imagePart.setContentID("image1");

		// 邮件正文
		MimeMultipart htmlMP = new MimeMultipart("alternative");
		MimeBodyPart htmlContent = new MimeBodyPart();
		// cid = imageBody.setContentID()中设置的值
		htmlContent.setContent("<span style='color:red;'>使用JavaMail发送含有图片的Html邮件<img src=\"cid:image1\"/></span>",
				"text/html;charset=utf-8");
		htmlMP.addBodyPart(htmlContent);
		htmlPart.setContent(htmlMP);

		message.setContent(mailBodyMP);

		Transport.send(message);
		// 将邮件写出到本地, 可以使用FoxMail工具打开查看
		// message.writeTo(new FileOutputStream("htmlImageMail.eml"));
	}

	@Test
	public void testSendHtmlMail() throws Exception {
		sendHtmlMail();
	}

	@Test
	public void testSendHtmlMailWithImage() throws Exception {
		sendHtmlMailWithImage();
	}
}
